<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Using kableExtra in Bookdown</title>
  <meta name="description" content="This example provides documentations for using kableExtra to customize tables in bookdown projects.">
  <meta name="generator" content="bookdown  and GitBook 2.6.7">

  <meta property="og:title" content="Using kableExtra in Bookdown" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="This example provides documentations for using kableExtra to customize tables in bookdown projects." />
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="Using kableExtra in Bookdown" />
  
  <meta name="twitter:description" content="This example provides documentations for using kableExtra to customize tables in bookdown projects." />
  

<meta name="author" content="Hao Zhu">


<meta name="date" content="2018-10-23">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="index.html">
<link rel="next" href="use-bootstrap-tables-in-gitbooks-epub.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />







<script src="libs/kePrint-0.0.1/kePrint.js"></script>
<link href="libs/bsTable-3.3.7/bootstrapTable.min.css" rel="stylesheet" />


<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; position: absolute; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; }
pre.numberSource a.sourceLine:empty
  { position: absolute; }
pre.numberSource a.sourceLine::before
  { content: attr(data-line-number);
    position: absolute; left: -5em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> Introduction</a></li>
<li class="chapter" data-level="2" data-path="cross-format-tables-in-bookdown.html"><a href="cross-format-tables-in-bookdown.html"><i class="fa fa-check"></i><b>2</b> Cross-format Tables in Bookdown</a><ul>
<li class="chapter" data-level="2.1" data-path="cross-format-tables-in-bookdown.html"><a href="cross-format-tables-in-bookdown.html#use-the-k-m-approach-instead-of-m-k"><i class="fa fa-check"></i><b>2.1</b> Use the “K-M” approach instead of “M-K”</a></li>
<li class="chapter" data-level="2.2" data-path="cross-format-tables-in-bookdown.html"><a href="cross-format-tables-in-bookdown.html#prepare-your-tables-for-all-formats"><i class="fa fa-check"></i><b>2.2</b> Prepare Your Tables for All Formats</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="use-bootstrap-tables-in-gitbooks-epub.html"><a href="use-bootstrap-tables-in-gitbooks-epub.html"><i class="fa fa-check"></i><b>3</b> Use Bootstrap Tables in gitbooks &amp; epub</a><ul>
<li class="chapter" data-level="3.1" data-path="use-bootstrap-tables-in-gitbooks-epub.html"><a href="use-bootstrap-tables-in-gitbooks-epub.html#gitbook"><i class="fa fa-check"></i><b>3.1</b> Gitbook</a></li>
<li class="chapter" data-level="3.2" data-path="use-bootstrap-tables-in-gitbooks-epub.html"><a href="use-bootstrap-tables-in-gitbooks-epub.html#epub"><i class="fa fa-check"></i><b>3.2</b> Epub</a></li>
</ul></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Using kableExtra in Bookdown</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="cross-format-tables-in-bookdown" class="section level1">
<h1><span class="header-section-number">Chapter 2</span> Cross-format Tables in Bookdown</h1>
<blockquote>
<p>Note that if kableExtra 0.9.0 doesn’t support the EPUB format. If you need to output tables in <code>.epub</code>, please upgrade to the dev version or version 1.0 on CRAN once it’s released.</p>
</blockquote>
<div id="use-the-k-m-approach-instead-of-m-k" class="section level2">
<h2><span class="header-section-number">2.1</span> Use the “K-M” approach instead of “M-K”</h2>
<p>Please read this chapter about the “K-M”/“M-K” approaches in <code>bookdown</code>:
<a href="https://bookdown.org/yihui/bookdown/new-session.html" class="uri">https://bookdown.org/yihui/bookdown/new-session.html</a></p>
<p>To generate cross-format tables with <code>kableExtra</code> in a multi-format bookdown project, you will have to use the “M-K” approach by setting <code>new_session: true</code> in <code>_bookdown.yml</code>. Somehow the “M-K” approach, which merges chapters to a big Rmd and then renders, shares the global environment across formats. For now, I’m not sure if this is a bug or intended behavior. It might be fixable in the future but please don’t count on that. By setting <code>new_session: true</code>, we force R to use a new session for every chapter for different formats. In this way, tables are generated differently in different formats.</p>
<p>Note that the “M-K” approach is slower than the “K-M” approach. At the same time, packages and data are not shared accross chapter.</p>
<pre><code># Example _bookdown.yml
book_filename: &quot;bookdown_example&quot;
delete_merged_file: true
new_session: true
language:
  ui:
    chapter_name: &quot;Chapter &quot;</code></pre>
</div>
<div id="prepare-your-tables-for-all-formats" class="section level2">
<h2><span class="header-section-number">2.2</span> Prepare Your Tables for All Formats</h2>
<p>In most cases, functions in <code>kable</code> and <code>kableExtra</code> use the same API to accomplish the same styling task in HTML and LaTeX. However, you also need some format specific settings so your tables will look good in both formats. Some common items here include the <code>booktabs</code> and <code>longtable</code> settings in <code>kable</code> and the <code>bootstrap_options</code> and <code>latex_options</code> in <code>kable_styling</code>.</p>
<p>Here is an example for a table that will work in HTML, LaTeX &amp; EPUB.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(kableExtra)
<span class="kw">library</span>(dplyr)</code></pre>
<pre><code>## Warning: package &#39;dplyr&#39; was built under R version 3.5.1</code></pre>
<pre><code>## 
## Attaching package: &#39;dplyr&#39;</code></pre>
<pre><code>## The following objects are masked from &#39;package:stats&#39;:
## 
##     filter, lag</code></pre>
<pre><code>## The following objects are masked from &#39;package:base&#39;:
## 
##     intersect, setdiff, setequal, union</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">options</span>(<span class="dt">kableExtra.html.bsTable =</span> T)
iris[<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>, ] <span class="op">%&gt;%</span>
<span class="st">  </span><span class="kw">mutate_if</span>(is.numeric, <span class="cf">function</span>(x) {
    <span class="kw">cell_spec</span>(x, <span class="dt">bold =</span> T, 
              <span class="dt">color =</span> <span class="kw">spec_color</span>(x, <span class="dt">end =</span> <span class="fl">0.9</span>),
              <span class="dt">font_size =</span> <span class="kw">spec_font_size</span>(x))
  }) <span class="op">%&gt;%</span>
<span class="st">  </span><span class="kw">mutate</span>(<span class="dt">Species =</span> <span class="kw">cell_spec</span>(
    Species, <span class="dt">color =</span> <span class="st">&quot;white&quot;</span>, <span class="dt">bold =</span> T,
    <span class="dt">background =</span> <span class="kw">spec_color</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>, <span class="dt">end =</span> <span class="fl">0.9</span>, 
                            <span class="dt">option =</span> <span class="st">&quot;A&quot;</span>, <span class="dt">direction =</span> <span class="dv">-1</span>)
  )) <span class="op">%&gt;%</span>
<span class="st">  </span><span class="kw">kable</span>(<span class="dt">escape =</span> F, <span class="dt">align =</span> <span class="st">&quot;c&quot;</span>, <span class="dt">booktabs =</span> T) <span class="op">%&gt;%</span>
<span class="st">  </span><span class="kw">kable_styling</span>(<span class="kw">c</span>(<span class="st">&quot;striped&quot;</span>, <span class="st">&quot;condensed&quot;</span>), 
                <span class="dt">latex_options =</span> <span class="st">&quot;striped&quot;</span>, 
                <span class="dt">full_width =</span> F)</code></pre>
<table class="table table-striped table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:center;">
Sepal.Length
</th>
<th style="text-align:center;">
Sepal.Width
</th>
<th style="text-align:center;">
Petal.Length
</th>
<th style="text-align:center;">
Petal.Width
</th>
<th style="text-align:center;">
Species
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(40, 174, 128, 1);font-size: 14px;">5.1</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(31, 154, 138, 1);font-size: 13px;">3.5</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(62, 75, 138, 1);font-size: 10px;">1.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(53, 96, 141, 1);font-size: 11px;">0.2</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(254, 206, 145, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(37, 131, 142, 1);font-size: 12px;">4.9</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(72, 34, 116, 1);font-size: 9px;">3</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(62, 75, 138, 1);font-size: 10px;">1.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(53, 96, 141, 1);font-size: 11px;">0.2</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(254, 160, 109, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(57, 87, 140, 1);font-size: 10px;">4.7</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(56, 88, 140, 1);font-size: 10px;">3.2</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(68, 1, 84, 1);font-size: 8px;">1.3</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(53, 96, 141, 1);font-size: 11px;">0.2</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(246, 110, 92, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(67, 62, 133, 1);font-size: 10px;">4.6</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(67, 62, 133, 1);font-size: 10px;">3.1</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(37, 131, 142, 1);font-size: 12px;">1.5</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(53, 96, 141, 1);font-size: 11px;">0.2</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(222, 73, 104, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(31, 154, 138, 1);font-size: 13px;">5</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(41, 175, 127, 1);font-size: 14px;">3.6</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(62, 75, 138, 1);font-size: 10px;">1.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(53, 96, 141, 1);font-size: 11px;">0.2</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(183, 55, 121, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(187, 223, 39, 1);font-size: 16px;">5.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(187, 223, 39, 1);font-size: 16px;">3.9</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(187, 223, 39, 1);font-size: 16px;">1.7</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(187, 223, 39, 1);font-size: 16px;">0.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(140, 41, 129, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(67, 62, 133, 1);font-size: 10px;">4.6</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(37, 131, 142, 1);font-size: 12px;">3.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(62, 75, 138, 1);font-size: 10px;">1.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(34, 168, 132, 1);font-size: 13px;">0.3</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(100, 26, 128, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(31, 154, 138, 1);font-size: 13px;">5</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(37, 131, 142, 1);font-size: 12px;">3.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(37, 131, 142, 1);font-size: 12px;">1.5</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(53, 96, 141, 1);font-size: 11px;">0.2</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(60, 15, 112, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(68, 1, 84, 1);font-size: 8px;">4.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(68, 1, 84, 1);font-size: 8px;">2.9</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(62, 75, 138, 1);font-size: 10px;">1.4</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(53, 96, 141, 1);font-size: 11px;">0.2</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(20, 14, 54, 1);">setosa</span>
</td>
</tr>
<tr>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(37, 131, 142, 1);font-size: 12px;">4.9</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(67, 62, 133, 1);font-size: 10px;">3.1</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(37, 131, 142, 1);font-size: 12px;">1.5</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: rgba(68, 1, 84, 1);font-size: 8px;">0.1</span>
</td>
<td style="text-align:center;">
<span style=" font-weight: bold;    color: white;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(0, 0, 4, 1);">setosa</span>
</td>
</tr>
</tbody>
</table>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="index.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="use-bootstrap-tables-in-gitbooks-epub.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"google": false,
"linkedin": false,
"weibo": false,
"instapper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"download": ["bookdown_example.pdf", "bookdown_example.epub"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

</body>

</html>
